import { _decorator, Component, Node } from 'cc';
import { BaseManager } from './BaseManager';
const { ccclass, property } = _decorator;

@ccclass('WheelManager')
export class WheelManager extends BaseManager {

    managerName: string = '轮子管理器'

    init(): void {
        
    }

    /**
     * 取随机整数
     * 左闭右开
     * @param min 最小值，默认为0
     * @param max 最大值，默认为1
     * @returns 
     */
    getRandomInt(min: number = 0, max: number = 1): number {
        if (min == max) {
            return min
        }
        let m1 = min > max ? max : min
        let m2 = max < min ? min : max
        return Math.floor(Math.random() * (m2 - m1)) + m1
    }

    /**
     * 获取二维数组中边界的index
     * @param x 横向长度
     * @param y 纵向长度
     * @returns index数组
     */
    getBorderIndex(x: number, y: number): number[] {
        let rounds: number[] = []
        for (let i = 0; i < x; i++) {
            rounds.push(i)
            rounds.push((y - 1) * x + i)
            if (i == 0 || i == x - 1) {
                for (let c = 1; c < y - 1; c++) {
                    rounds.push(c * y + i)
                }
            }
        }
        return rounds
    }

    /**
     * 将秒数转换为HH:MM:SS格式
     * @param seconds 秒
     * @returns HH:MM:SS
     */
    secondsToHourTime(seconds: number): string {
        let minutes = Math.floor(seconds / 60)
        let secs = seconds % 60
        let hours = Math.floor(minutes / 60)
        minutes = minutes % 60
        let h = hours < 10 ? '0' + hours : hours
        let m = minutes < 10 ? '0' + minutes : minutes
        let s = secs < 10 ? '0' + secs : secs 
        return h + ':' + m + ':' + s
    }

    /**
     * 将秒数转换为MM:SS格式
     * @param seconds 秒
     * @returns MM:SS
     */
    secondsToMinuteTime(seconds: number) {
        let minutes = Math.floor(seconds / 60)
        let secs = seconds % 60
        let m = minutes < 10 ? '0' + minutes : minutes
        let s = secs < 10 ? '0' + secs : secs
        return m + ':' + s
    }

    timestampToMSMS(timestamp: number) {
        let m = Math.floor(timestamp / (1000 * 60))
        let s = timestamp % (1000 * 60) / 1000
        let ms = timestamp % 1000
        let result = (m < 10 ? '0' : '') + m + ':' + (s < 10 ? '0' : '') + s + ':' + (ms < 10 ? '00' : (ms < 100 ? '0' : '')) + ms
        return result
    }

    timestampToHMSMS(timestamp: number) {
        let h = Math.floor((timestamp % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
        let m = Math.floor((timestamp % (1000 * 60 * 60)) / (1000 * 60))
        let s = Math.floor((timestamp % (1000 * 60)) / 1000)
        let ms = Math.floor(timestamp % 1000)
        let result = (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m + ':' + (s < 10 ? '0' : '') + s + ':' + (ms < 10 ? '00' : (ms < 100 ? '0' : '')) + ms
        return result
    }

    /**
     * 判断两个时间戳是否为同一天
     * @param time 时间戳1
     * @param check 时间戳2，默认为现在
     * @returns 是否为同一天
     */
    isTheSameDay(time: number, check: number = Date.now()) {
        let t1 = new Date(time).setHours(0, 0, 0, 0)
        let t2 = new Date(check).setHours(0, 0, 0, 0)
        return t1 === t2
    }

    /**
     * 从数组中获得随机元素
     * @param array 数组
     * @returns 元素
     */
    getRandomItemInArray(array: any[]) {
        return array[this.getRandomInt(0, array.length)]
    }
}


